<?PHP
class MHDEV_View_Helper_GetLastVisits extends Zend_View_Helper_Abstract
{
	/*
	 * Get a graphic of the latest visits of x days
	 * 
	 * @param int $number_of_days
	 * @param string $chart_type
	 * 
	 * @return string chart image from google-chart
	 */
	public function GetLastVisits($number_of_days, $chart_type, $get_array = FALSE)
	{
		if(!$number_of_days OR $number_of_days == 0) { $number_of_days = 7; }
		
		// vars
		$i 		= 1;
		$chd 	= FALSE;
		$chxr	= FALSE;
		$day	= FALSE;
		
		// db
		$user_online_count	= new UserOnlineCount();
		
		// query
		$select = $user_online_count->select()->setIntegrityCheck(false);
		$select->from(
				array('p' => 'user_online_count')
			)
		->columns('COUNT(ip_address) AS total')
		->where('date BETWEEN DATE_SUB(CURDATE(), INTERVAL ' . $number_of_days . ' DAY) AND CURDATE()')
		->group('DATE_FORMAT(`date`, "%Y-%m-%d")')
		->order('date');
		
		// results
		$results	= $user_online_count->fetchAll($select);
		
		if($results->count() > 0)
		{
			foreach($results->toArray() AS $res)
			{
				$chd 	.= $res['total'];
				$chxr 	.= $res['total'];
				$day	.= $i;
				
				if($i < $results->count()) 
				{ 
					$chd	.= ','; 
					$chxr	.= ',';
					$day	.= ',';
				}
				
				$i++;
			}
			
			$google_url 	= 'http://chart.apis.google.com/chart?chs=300x100&cht=ls&chd=t:' . $chd . '&chxt=y&chxr=' . $chxr;
			$content 		= '<img src="' . $google_url . '" />';
		}
		else
		{
			$content = false;
		}
		
		# SELECT `uid`, `date`, COUNT(`ip`) AS `total` 
		# FROM `user_statistics` 
		# WHERE `date` BETWEEN 
		# DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE() 
		# GROUP BY DATE_FORMAT(`date`, "%Y-%m-%d") ORDER BY `date`
		
		if($get_array)
		{
			return $results->toArray();
		}
		else
		{
			return $content;
		}
	}
}
?>